package cn.tedu.csmall.passport.controller;

import cn.tedu.csmall.passport.pojo.dto.AdminAddNewDTO;
import cn.tedu.csmall.passport.pojo.dto.AdminLoginDTO;
import cn.tedu.csmall.passport.pojo.vo.AdminListItemVO;
import cn.tedu.csmall.passport.security.LoginPrincipal;
import cn.tedu.csmall.passport.service.IAdminService;
import cn.tedu.csmall.passport.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = "01  管理员信息模块")
@Slf4j
@Validated
@RestController
@RequestMapping("/admins")
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class AdminController {
    @Autowired
    IAdminService adminService;

    @PostMapping("/login")
    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 100)
    public JsonResult<String> login(AdminLoginDTO adminLoginDTO){
        log.debug("开始处理请求登录业务: {}",adminLoginDTO);
        String jwt = adminService.login(adminLoginDTO);
        return JsonResult.ok(jwt);
    }


    @PostMapping("add-new")
    @PreAuthorize("hasAuthority('/ams/admin/add-new')")
    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Validated AdminAddNewDTO adminAddNewDTO){
        log.debug("{}",adminAddNewDTO);
        adminService.addNew(adminAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除管理员")
    @PreAuthorize("hasAuthority('/ams/admin/delete')")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "管理员id", required = true, dataType = "long")
    public JsonResult<Void> delete(@Range(min = 1,message = "删除管理员失败 id无效") @PathVariable Long id){
        adminService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用管理员")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "管理员id", required = true, dataType = "long")
    public JsonResult<Void> setEnable(@Range(min = 1,message = "启用管理员失败 id无效") @PathVariable Long id){
        adminService.setEnable(id);
        return JsonResult.ok();
    }


    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用管理员")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParam(name = "id", value = "管理员id", required = true, dataType = "long")
    public JsonResult<Void> setDisable(@Range(min = 1,message = "禁用管理员失败 id无效") @PathVariable Long id){
        adminService.setDisable(id);
        return JsonResult.ok();
    }

    @GetMapping("")
    @ApiOperation("查询管理员列表")
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    @ApiOperationSupport(order = 400)
    public JsonResult<List<AdminListItemVO>> list(@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        log.debug("当事人是: {}",loginPrincipal);
        List<AdminListItemVO> list = adminService.list();
        return JsonResult.ok(list);
    }
}
